﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Collections;

namespace RuleCode.ObjectTree
{
    class Util_LabelEditor
    {
        private static Util_LabelEditor instance;

        private string labelFileID;
        private string labelFileLanguage;

        #region 构造函数
        public Util_LabelEditor()
        {
            this.labelFileID = "GCN";
        }

        public Util_LabelEditor(string language)
            : this()
        {
            this.labelFileLanguage = language;
        }


        public Util_LabelEditor(string fileID, string fileLan)
        {
            this.labelFileID = fileID;
            this.labelFileLanguage = fileLan;
        }
        #endregion

        #region Instance 调用
        public static Util_LabelEditor Instance
        {
            get
            {
                if (instance == null)
                    instance = new Util_LabelEditor();
                return instance;
            }
            set
            {
                instance = value;
            }
        }
        #endregion


        #region 多语言文件路径存储
        /// <summary>
        /// 语言文件前缀 "RuleCode"
        /// </summary>
        public string LanguageFilePrefix
        {
            get { return "RuleCode"; }
        }

        /// <summary>
        /// 获取或设置 当前语言编码,默认为"CH-ZH"
        /// </summary>
        [DefaultValue("CH-ZH")]
        public string CurrentLanguage
        {
            get
            {
                if (this.labelFileLanguage == null || this.labelFileLanguage == "")
                    return "ch-zh";
                //return "en-us";
                else
                    return this.labelFileLanguage;
            }
            set { this.labelFileLanguage = value; }
        }

        /// <summary>
        /// 获取或设置当前写入文件的文件编码("GCN","SYS"...)
        /// </summary>
        public string LabelLanFileID
        {
            get { return this.labelFileID; }
            set { this.labelFileID = value; }
        }

        /// <summary>
        /// 获取标签文件的路径
        /// </summary>
        public string LabelFilePath
        {
            get
            {
                //return Util_DTE.AddIn_SetUpPath  + "LabelFile";
                return Util.GetLabelFilePath.TrimEnd("\\".ToCharArray());// +"\\LabelFile";
            }
        }

        public string LanguageCategoryFileName
        {
            get
            {                                                                       
                return LabelFilePath + "\\LanguageCategory.txt";
            }
        }
        #endregion

        #region 获取语言种类列表
        /// <summary>
        /// 获取语言种类列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetLanguageList()
        {
            List<string> languageList = new List<string>();
            
            StreamReader sReader = new StreamReader(LanguageCategoryFileName);
            while (!sReader.EndOfStream)
            {
                languageList .Add ( sReader.ReadLine());
                sReader.ReadLine();
            }
            sReader.Close();

            return languageList;
        }
        #endregion

        #region 一些方法
        /// <summary>
        /// 根据标签ID 得出当前 标签语言  文件里相应的  标签内容
        /// </summary>
        /// <param name="labelID"></param>
        /// <returns></returns>
        public string GetLabel(string labelID)
        {
            if (labelID == "")
            {
                return "";
            }
            return this.GetLabel(labelID, this.CurrentLanguage);
        }


        /// <summary>
        /// 根据 LabelID 取得 在 languageID 对应的文件中的 label
        /// </summary>
        /// <param name="labelID">标签ID @GCN001，@SYS001</param>
        /// <param name="languageID"> 语言文件标志: ch-zh， en-us...</param>
        /// <returns></returns>
        public string GetLabel(string labelID, string languageID)
        {
            try
            {


                string labelLanguage = languageID;

                //labelID的格式是  @GCN234
                string labelFileID = labelID.Substring(1, 3);

                string labelFileName = this.LabelFilePath + "\\" + this.LanguageFilePrefix + labelFileID + labelLanguage + ".txt";

                StreamReader sReader = new StreamReader(labelFileName, Encoding.Default, true);

                while (!sReader.EndOfStream)
                {
                    string lineString = sReader.ReadLine();
                    string description = "";
                    if (sReader.Peek() != '@')
                        description = sReader.ReadLine();

                    string filelabelId;
                    string filelabel = "";
                    int separatorIndex = lineString.IndexOf(' ');
                    if (separatorIndex > 0)
                    {
                        filelabelId = lineString.Substring(0, separatorIndex);
                        filelabel = lineString.Substring(separatorIndex + 1, lineString.Length - 1 - separatorIndex);
                    }
                    else
                    {
                        filelabelId = lineString.Trim();
                        filelabel = "";
                    }
                    if (filelabelId == labelID)
                        return filelabel;

                }

                return labelID;
            }
            catch (Exception)
            {

                return labelID;
            }
        }

        /// <summary>
        /// 根据 标签内容 标签描述 标签语言ID 标签语言文件ID 获取 标签ID         /// 
        /// </summary>
        /// <param name="label">标签内容</param>
        /// <param name="labelDescription">标签描述</param>
        /// <param name="languageID">标签语言ID</param>
        /// <param name="languageFileID">标签语言文件ID </param>
        /// <returns>标签ID</returns>
        public string GetLabelID(string label, string labelDescription, string languageID, string languageFileID)
        {

            string labelFileName = this.LabelFilePath + "\\" + this.LanguageFilePrefix + languageFileID + languageID + ".txt";


            StreamReader sReader = new StreamReader(labelFileName, Encoding.Default, true);

            while (!sReader.EndOfStream)
            {
                string lineString = sReader.ReadLine();
                string description = "";
                if (sReader.Peek() != '@')
                    description = sReader.ReadLine();

                string filelabelId;
                string filelabel = "";
                int separatorIndex = lineString.IndexOf(' ');
                if (separatorIndex > 0)
                {
                    filelabelId = lineString.Substring(0, separatorIndex);
                    filelabel = lineString.Substring(separatorIndex + 1, lineString.Length - 1 - separatorIndex);
                }
                else
                {
                    filelabelId = lineString.Trim();
                    filelabel = "";
                }
                if (filelabel == label && description == labelDescription)
                    return filelabelId;
            }

            return "";
        }

        /// <summary>
        /// 根据 标签内容 和 标签描述 获取 标签ID
        /// </summary>
        /// <param name="label">标签内容</param>
        /// <param name="labelDescription">标签描述</param>
        /// <returns>标签ID</returns>
        public string GetLabelID(string label, string labelDescription)
        {
            string languageID = this.CurrentLanguage;
            string languageFileID = this.labelFileID;
            return this.GetLabelID(label, labelDescription, languageID, languageFileID);
        }

        /// <summary>
        /// 根据 标签Label ,语言ID languageID,语言文件ID languageFileID
        /// </summary>
        /// <param name="label">标签内容</param>
        /// <param name="languageID">语言ID : ch-zh,en-us,...</param>
        /// <param name="languageFileID">语言文件ID: GCN ,SYS</param>
        /// <returns></returns>
        public List<string> GetLabelID(string label, string languageID, string languageFileID)
        {
            List<string> labelIDList = new List<string>();

            string labelFileName = this.LabelFilePath + "\\" + this.LanguageFilePrefix + languageFileID + languageID + ".txt";


            StreamReader sReader = new StreamReader(labelFileName, Encoding.Default, true);

            while (!sReader.EndOfStream)
            {
                string lineString = sReader.ReadLine();
                string description = "";
                if (sReader.Peek() != '@')
                    description = sReader.ReadLine();

                string filelabelId;
                string filelabel = "";
                int separatorIndex = lineString.IndexOf(' ');
                if (separatorIndex > 0)
                {
                    filelabelId = lineString.Substring(0, separatorIndex);
                    filelabel = lineString.Substring(separatorIndex + 1, lineString.Length - 1 - separatorIndex);
                }
                else
                {
                    filelabelId = lineString.Trim();
                    filelabel = "";
                }
                if (filelabel == label)
                    labelIDList.Add(filelabelId);
            }

            return labelIDList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="labelDescription"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        public string GetLabelIDInAllFile(string label, string labelDescription, string languageID)
        {
            DirectoryInfo dInfo = new DirectoryInfo(this.LabelFilePath);
            string searchPattern = "RuleCode" + "*" + languageID + ".txt";

            FileInfo[] fInfos = dInfo.GetFiles(searchPattern);
            foreach (FileInfo fInfo in fInfos)
            {
                StreamReader sReader = new StreamReader(fInfo.FullName, Encoding.Default, true);

                while (!sReader.EndOfStream)
                {
                    string lineString = sReader.ReadLine();
                    string description = "";
                    if (sReader.Peek() != '@')
                        description = sReader.ReadLine();

                    string filelabelId;
                    string filelabel = "";
                    int separatorIndex = lineString.IndexOf(' ');
                    if (separatorIndex > 0)
                    {
                        filelabelId = lineString.Substring(0, separatorIndex);
                        filelabel = lineString.Substring(separatorIndex + 1, lineString.Length - 1 - separatorIndex);
                    }
                    else
                    {
                        filelabelId = lineString.Trim();
                        filelabel = "";
                    }

                    if (filelabel.Trim() == label && description == labelDescription)
                        return filelabelId;

                }
            }
            return "";
        }
        #endregion


        public string FindOrCreateLabelID(string labelText, string languageID)
        {
            string labelID = string.Empty;
            Hashtable hTable = new Hashtable();

            DirectoryInfo dInfo = new DirectoryInfo(this.LabelFilePath);
            string searchPattern = "RuleCode" + "*" + languageID + ".txt";

            FileInfo[] fInfos = dInfo.GetFiles(searchPattern);
            foreach (FileInfo fInfo in fInfos)
            {
                StreamReader sReader = new StreamReader(fInfo.FullName, Encoding.Default, true);
                int index =1;

                while (!sReader.EndOfStream)
                {
                    string lineString = sReader.ReadLine();
                    string description = "";
                    if (sReader.Peek() != '@')
                        description = sReader.ReadLine();

                    string filelabelId;
                    string filelabel = "";
                    int separatorIndex = lineString.IndexOf(' ');
                    if (separatorIndex > 0)
                    {
                        filelabelId = lineString.Substring(0, separatorIndex);
                        filelabel = lineString.Substring(separatorIndex + 1, lineString.Length - 1 - separatorIndex);
                    }
                    else
                    {
                        filelabelId = lineString.Trim();
                        filelabel = "";
                    }

                    if (filelabel.Trim() == labelText)
                    {
                        labelID = filelabelId;
                        return labelID ;
                    }
                    index=Convert .ToInt32 (filelabelId .Substring (4,filelabelId .Length -4))+1;
                }

                hTable.Add(fInfo.FullName.ToUpper (), index);
            }

            //没找到
            string defaultFileName = this.LabelFilePath + "\\" + this.LanguageFilePrefix + this.LabelLanFileID + languageID + ".txt";
            if (hTable[defaultFileName.ToUpper ()] == null)
            {
                throw new InvalidOperationException("没有找到标签ID，插入失败！");
            }
            else
            {
                labelID = "@" + this.LabelLanFileID + hTable[defaultFileName.ToUpper ()].ToString();
                
                searchPattern = this.LanguageFilePrefix + this.LabelLanFileID + "*.txt";
                FileInfo[] resultInfos = dInfo.GetFiles(searchPattern);

                foreach (FileInfo resultInfo in resultInfos)
                {
                    if (defaultFileName.ToUpper () == resultInfo .FullName.ToUpper () )
                    {
                        StreamWriter sWriter = new StreamWriter(resultInfo.FullName, true, Encoding.Default);
                        sWriter.WriteLine(labelID + " " + labelText);
                        sWriter.Close();                        
                    }
                    else
                    {
                        StreamWriter sWriter = new StreamWriter(resultInfo.FullName, true, Encoding.Default);
                        sWriter.WriteLine(labelID + " ");
                        sWriter.Close();
                    }
                }
            }

            return labelID;
        }

        public void Set()
        {
            string gcnenFileName = "D:\\项目相关\\Work\\RuleCode.WinMain\\bin\\Debug\\Data\\LabelFile\\RuleCodeGCNen-us.txt";

            int endIndex = 1156;
            StreamWriter sWriter = new StreamWriter(gcnenFileName, true, Encoding.Default);
            for (int index = 1114; index < endIndex;index++ )
            {
                string labelID = "@GCN" + index.ToString();
                sWriter.WriteLine(labelID);
            }
            sWriter.Close();
        }
    }
}
